﻿using LoveEat.API.Write.Applications.Commands.Goods;
using LoveEat.Domain.Goods;
using LoveEat.ErrorCode;
using LoveEat.Infrastructure.Interfaces;
using MediatR;

namespace LoveEat.API.Write.Applications.CommandHandlers.Goods
{
    public class BatchDeleteCommandHandler : IRequestHandler<BatchDeleteCommand, APIResult<int>>
    {
        private readonly IBaseRespository<GoodsModel> goodsRep;

        public BatchDeleteCommandHandler(IBaseRespository<GoodsModel> goodsRep)
        {
            this.goodsRep = goodsRep;
        }

        public async Task<APIResult<int>> Handle(BatchDeleteCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            result.Code=APIResultCode.删除成功;
            result.Message=APIResultCode.删除成功.ToString();

            var goodsdelete = goodsRep.GetAll().Where(x => request.GoodsId.Contains(x.GoodsId));
            foreach (var good in goodsdelete)
            {
                good.IsDelete=true;

            }
            await goodsRep.UpdateRange(goodsdelete);
            return await Task.FromResult(result);
        }
    }
}
